﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        lbTenDN.Text = Session["tendn"].ToString();
        lbTenDHan.Text = Session["tendn"].ToString();
        if (!IsPostBack)
        {
            if (Session["tendn"] == "")
                Response.Redirect("~/dangnhap.aspx");
            if (Request.QueryString["Msp"] != null)
            {
                int MaSP = int.Parse(Request.QueryString["Msp"]);
                DataTable dt = XLDL.Docbang("Select tensp,dongia from SanPham where masp=" + MaSP);
                string TenSP = dt.Rows[0][0].ToString();
                int dongia = int.Parse(dt.Rows[0][1].ToString());
                int soluong = 1;
                ThemVaoGioHang(MaSP, TenSP, dongia, soluong);
            }
            if (Session["giohang"] != null)
            {
                DataTable dt = new DataTable();
                dt = (DataTable)Session["giohang"];
                System.Decimal tongThanhtien = 0;
                foreach (DataRow r in dt.Rows)
                {
                    r["Thanhtien"] = Convert.ToInt32(r["SoLuong"]) * Convert.ToDecimal(r["Dongia"]);
                    tongThanhtien += Convert.ToDecimal(r["Thanhtien"]);
                    lbTongThanhTien.Text = tongThanhtien.ToString();
                }
                gdvgiohang.DataSource = dt;
                gdvgiohang.DataBind();
            }
            else
            {
                PnlGiohangan.Visible = true;
                PnlGiohanghien.Visible = false;
            }

        }

    }

    public void ThemVaoGioHang(int MaSP, string TenSP, int Dongia, int Soluong)
    {
        DataTable dt;
        if (Session["giohang"] == null)
        {
            dt = new DataTable();
            dt.Columns.Add("MaSP");
            dt.Columns.Add("TenSP");
            dt.Columns.Add("Dongia");
            dt.Columns.Add("SoLuong");
            dt.Columns.Add("Thanhtien");
        }
        else
        {
            dt = (DataTable)Session["giohang"];
        }
        int dong = Spdacotronggiohang(MaSP, dt);
        if (dong != -1)
        {
            dt.Rows[dong]["Soluong"] = Convert.ToInt32(dt.Rows[dong]["Soluong"]) + Soluong;
        }
        else
        {
            DataRow dr = dt.NewRow();
            dr["MaSP"] = MaSP;
            dr["TenSP"] = TenSP;
            dr["Dongia"] = Dongia;
            dr["SoLuong"] = Soluong;
            dr["Thanhtien"] = Dongia * Soluong;
            dt.Rows.Add(dr);
        }
        Session["giohang"] = dt;
    }

    public static int Spdacotronggiohang(int MaSP, DataTable dt)
    {
        int dong = -1;
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            if (int.Parse(dt.Rows[i]["MaSP"].ToString()) == MaSP)
            {
                dong = i;
                break;
            }
        }
        return dong;
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Nutxoa")
        {
            int chiso = int.Parse(e.CommandArgument.ToString());
            try
            {
                DataTable dt = (DataTable)Session["giohang"];
                dt.Rows.RemoveAt(chiso);
                Session["giohang"] = dt;
                Response.Redirect("~/giohang.aspx");
            }
            catch
            {
                Response.Redirect("~/giohang.aspx");
            }
        }
    }
    protected void btXoagiohang_Click(object sender, EventArgs e)
    {
        Session["giohang"] = null;
        Response.Redirect("~/trangchu.aspx");
    }
    protected void btCapnhat_Click(object sender, EventArgs e)
    {
        DataTable dt = (DataTable)Session["giohang"];
        foreach (GridViewRow r in gdvgiohang.Rows)
        {
            foreach (DataRow dr in dt.Rows)
            {
                if (Convert.ToString(gdvgiohang.DataKeys[r.DataItemIndex].Value) == dr["MaSP"].ToString())
                {
                    TextBox t = (TextBox)r.Cells[3].FindControl("txtSoluong");
                    if (Convert.ToInt32(t.Text) <= 0)
                        dt.Rows.Remove(dr);
                    else
                        dr["SoLuong"] = t.Text;
                    break;
                }
            }
        }
        Session["giohang"] = dt;
        Response.Redirect("~/giohang.aspx");
    }
    protected void btDatHang_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/thanhtoan.aspx");
    }
    protected void btTieptucmua_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/trangchu.aspx");
    }

}